package com.china08.yxyapi.api.manage.tec.teaching.lesson.cloud;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLesson;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonItemRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class TecCloudLessonItemDelController extends YxyApiBaseController {

    @Autowired
    YxyLessonsCloudLessonItemRepository itemRepository;

    @Autowired
    YxyLessonsCloudLessonRepository lessonRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lesson/item/del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String itemId = reqModel.itemId;
        YxyLessonsCloudLessonItem item = itemRepository.findById(itemId);
        if (item != null) {
            item.setStatus(-1);
            itemRepository.saveAndFlush(item);

            // 修改课程的更新时间
            String lessonId = item.getLessonId();
            YxyLessonsCloudLesson lesson = lessonRepository.findById(lessonId);
            if (lesson != null) {
                lesson.setUpdateDate(new Date());
                lessonRepository.saveAndFlush(lesson);
            }
        }
    }

    public static class ReqModel extends YxyApiReqModel {
        public String itemId;
    }
}
